package com.taotao.st3g8.base;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

/**
 * ajax 返回给前端的数据 模型
 */
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class RespData<T> {

	/**
	 * 成功
	 */
	public static final int SUCCEED_CODE = 1;
	/**
	 * 失败
	 */
	public static final int FAIL_CODE = 0;

	/**
	 * 重新登录
	 */
	public static final int RE_LOGIN_CODE = -1;

	/**
	 * 状态码
	 */
	@Schema(description="状态码；1-成功，0-失败，-1-重新登录")
	private int code;

	/**
	 * 错误描述
	 */
	@Schema(description="错误描述")
	private String msg;

	/**
	 * 调试的提示
	 */
	@Schema(description="调试的提示")
	private String debugInfo;

	/**
	 * 返回数据
	 */
	@Schema(description="返回数据")
	private T data;


	public RespData(int code, String msg, T data){
		this.code= code;
		this.msg = msg;
		this.data = data;
	}

	public boolean isSuccess(){
		return SUCCEED_CODE == code;
	}

	public boolean isFailed(){
		return FAIL_CODE == code;
	}

	public boolean isReLogin(){
		return RE_LOGIN_CODE == code;
	}


	@Override
	public String toString() {
		return ToStringBuilder.reflectionToString(this, ToStringStyle.JSON_STYLE);
	}

	public static <E> RespData<E> successResp(E data) {
		return new RespData<E>(SUCCEED_CODE, null, data);
	}

	public static <E> RespData<E> successResp() {
		return new RespData<E>(SUCCEED_CODE, null, null);
	}

	public static <E> RespData<E> failedResp(String msg) {
		return new RespData<E>(FAIL_CODE, msg, null);
	}


	public static <E> RespData<E> reLoginResp() {
		return new RespData<E>(RE_LOGIN_CODE, "请重新登录", null);
	}



}